package com.yj;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfiguration {
    /**
     * 创建主数据源的配置对象
     */
    @Bean(name = "serviceDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.service")//加载spring.datasource.service前缀的配置，自动配置DataSourceProperties对象
    public DataSourceProperties serviceDataSourceProperties() {
        DataSourceProperties properties = new DataSourceProperties();
//        properties.setUrl("jdbc:mysql://127.0.0.1:3306/task-demo?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
//        properties.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        properties.setUsername("root");
//        properties.setPassword("123456");
        return properties;
    }

    /**
     * 创建主数据源
     */
    @Primary//标明主数据源
    @Bean(name = "serviceDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.service.hikari")//加载spring.datasource.service.hikari前缀的配置，自动配置HikariDataSource对象
    public DataSource serviceDataSource() {
        DataSourceProperties properties = serviceDataSourceProperties();
        return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    /**
     * 创建quartz数据源的配置对象
     */
    @Bean(name = "quartzDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.quartz")
    public DataSourceProperties quartzDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * 创建quartz数据源
     */
    @QuartzDataSource//标明quartz数据源
    @Bean(name = "quartzDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.quartz.hikari")
    public DataSource quartzDataSource() {
        DataSourceProperties properties = quartzDataSourceProperties();
        return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }
}
